//
//  MyViewController.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/5.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "MyViewController.h"
#import "MyTypeOneCell.h"
#import "MyTypeTwoCell.h"
#import "UIColor+HexM.h"
#import "MyHeaderView.h"

@interface MyViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *titleArray;
@end

@implementation MyViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[[UIImage imageNamed:@"tabbar_recom_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"tabbar_recom_1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我的";
    
    [self initData];
    [self setTableViewUI];
}

- (void)initData {
    
}

- (void)setTableViewUI {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MyTypeOneCell" bundle:nil] forCellReuseIdentifier:@"MyTypeOneCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyTypeTwoCell" bundle:nil] forCellReuseIdentifier:@"MyTypeTwoCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
//    MyHeaderView *view = [[MyHeaderView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width / 4 * 3)];
    
//    MyHeaderView *view = (MyHeaderView *)[[NSBundle mainBundle] loadNibNamed:@"MyHeaderView" owner:nil options:nil];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width / 3 * 1)];
    MyHeaderView *view = [[MyHeaderView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width / 3 * 1)];
    
//    NSArray *nibView =  [[NSBundle mainBundle] loadNibNamed:@"MyHeaderView" owner:self options:nil];
//    UIView *backView = [nibView objectAtIndex:0];
    
    NSLog(@"区头高度:%f--%f", view.frame.size.width, view.frame.size.height);
    [headerView addSubview:view];
    self.tableView.tableHeaderView = headerView;
}

#pragma mark --tableViewDelegate--
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }else if(section == 1) {
        return 1;
    }else if(section == 2) {
        return 2;
    }else if(section == 3) {
        return 1;
    }else if(section == 4) {
        return 2;
    }else if(section == 5) {
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 2 || indexPath.section == 4) {
        MyTypeOneCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyTypeOneCell"];
        if (!cell) {
            cell = (MyTypeOneCell *)[[NSBundle mainBundle] loadNibNamed:@"MyTypeOneCell" owner:nil options:nil];
        }
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                cell.nameLable.text = @"我的信息";
            }else if (indexPath.row == 1) {
                cell.nameLable.text = @"我的订单";
            }else if (indexPath.row == 2) {
                cell.nameLable.text = @"我的优惠卷";
            }
        }else if (indexPath.section == 2) {
            if (indexPath.row == 0) {
                cell.nameLable.text = @"相册";
            }else if (indexPath.row == 1) {
                cell.nameLable.text = @"我的关注";
            }
        }else if (indexPath.section == 4) {
            if (indexPath.row == 0) {
                cell.nameLable.text = @"关于App";
            }else if (indexPath.row == 1) {
                cell.nameLable.text = @"设置";
            }
        }
        return cell;
    }else if (indexPath.section == 1 || indexPath.section == 3 || indexPath.section == 5) {
        MyTypeTwoCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyTypeTwoCell"];
        if (!cell) {
            cell = (MyTypeTwoCell *)[[NSBundle mainBundle] loadNibNamed:@"MyTypeTwoCell" owner:nil options:nil];
        }
        cell.color = [UIColor colorWithHexString:@"#E1E0E7"];
        return cell;
    }else {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 2 || indexPath.section == 4) {
        return 50;
    }else if (indexPath.section == 1 || indexPath.section == 3 || indexPath.section == 5) {
        return 14;
    }else {
        return 0;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSLog(@"点击了第几个Cell");
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

@end
